package org.xielei.designpatterns.behavioral.strategy;

import org.xielei.designpatterns.DesignPatternConstants;

import java.util.concurrent.ConcurrentHashMap;

/**
 * StrategyFactory
 * 策略工厂
 *
 * @author xielei
 * @date 2020/8/6
 */
public class StrategyFactory {

    private static final ConcurrentHashMap<String, Strategy> STRATEGY_MAP = new ConcurrentHashMap<>();

    static {
        STRATEGY_MAP.put(DesignPatternConstants.STRATEGY_ONE_KEY, new ConcreteStrategyOne());
        STRATEGY_MAP.put(DesignPatternConstants.STRATEGY_TWO_KEY, new ConcreteStrategyTwo());
    }

    /**
     * 获取策略具体实现
     * @param key key
     * @return strategy
     */
    public Strategy getStrategy(String key) {
        return STRATEGY_MAP.get(key);
    }

    /**
     * 在具体实现注入到spring之后的注册到map的方法
     * @param key key
     * @param strategy strategy
     */
    public static void register(String key, Strategy strategy) {
        STRATEGY_MAP.put(key, strategy);
    }
}
